package com.newly.driver.sdk.device.video.cloud.channel;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.newly.driver.sdk.device.video.cloud.device.QueryDeviceResp;
import com.newly.driver.sdk.device.video.constants.DeviceConstant;
import com.newly.driver.sdk.device.video.service.impl.qiniu.AbstractQiNiuCloud;
import com.newly.driver.sdk.device.video.service.impl.tencent.AbstractTencentCloud;
import com.tencentcloudapi.iotvideoindustry.v20201201.models.DescribeIPCChannelsResponse;
import com.tencentcloudapi.iotvideoindustry.v20201201.models.GroupDeviceItem;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gjm guan
 * @date 2021/10/20 16:16
 * @desc 查询通道响应参数
 */
@Data
public class QueryChannelResp {
    /**
     * 通道总数
     */
    @ApiModelProperty("通道总数")
    private int totalCount;

    /**
     * 在线总数
     */
    @ApiModelProperty("在线总数")
    private int onlineCount;

    /**
     * 离线总数
     */
    @ApiModelProperty("离线总数")
    private int offlineCount;

    /**
     * 通道数据
     */
    @ApiModelProperty("通道数据")
    private List<Payload> payloads;


    public QueryChannelResp() {

    }

    public QueryChannelResp(QueryDeviceResp.Payload _payload) {
        this.totalCount = 1;
        if (_payload.getStatus() == 1) {
            this.onlineCount = 1;
        } else {
            this.offlineCount = 1;
        }

        this.payloads = new ArrayList<>(1);
        this.payloads.add(new Payload(_payload));
    }

    // tencent
    public QueryChannelResp(DescribeIPCChannelsResponse resp) {
        this.totalCount = resp.getTotalCount().intValue();
        GroupDeviceItem[] items = resp.getDeviceList();
        if (!ObjectUtil.isEmpty(items)) {
            this.payloads = Arrays.stream(items).map(item -> {
                // 在线和离线统计
                if (item.getStatus() == 1) {
                    this.onlineCount++;
                } else {
                    this.offlineCount++;
                }
                return new Payload(item);
            }).collect(Collectors.toList());
        } else {
            this.offlineCount = 0;
            this.onlineCount = 0;
            this.payloads = new ArrayList<>(0);
        }


    }

    // qiniu
    public QueryChannelResp(JSONObject jsonObject) {
        Integer total = jsonObject.getInt("total");
        Integer onlineCount = jsonObject.getInt("onlineCount");
        Integer offlineCount = jsonObject.getInt("offlineCount");

        this.totalCount = total;
        this.onlineCount = onlineCount;
        this.offlineCount = offlineCount;

        JSONArray items = jsonObject.getJSONArray("items");
        if (!ObjectUtil.isEmpty(items)) {
            this.payloads = items.stream()
                    .map(item -> new Payload((JSONObject) item))
                    .collect(Collectors.toList());
        }
    }


    /**
     * 通道数据
     */
    @Data
    public static class Payload {
        /**
         * 通道id
         */
        @ApiModelProperty("通道id")
        private String channelId;

        /**
         * 通道名称
         */
        @ApiModelProperty("通道名称")
        private String name;

        /**
         * 国标id
         */
        @ApiModelProperty("国标id")
        private String gbId;

        /**
         * 通道状态
         */
        @ApiModelProperty("通道状态")
        private DeviceConstant.DeviceState state;

        public Payload() {

        }

        public Payload(QueryDeviceResp.Payload _payload) {
            this.channelId = _payload.getDeviceId();
            this.name = _payload.getDeviceName();
            this.gbId = _payload.getGbId();
            if (_payload.getStatus() == 1) {
                this.state = DeviceConstant.DeviceState.ONLINE;
            } else {
                this.state = DeviceConstant.DeviceState.OFFLINE;
            }
        }

        public Payload(GroupDeviceItem groupDeviceItem) {
            this.channelId = groupDeviceItem.getDeviceId();
            this.name = groupDeviceItem.getNickName();
            this.gbId = groupDeviceItem.getDeviceCode();
            Long status = groupDeviceItem.getStatus();

            if (AbstractTencentCloud.TencentDeviceState.ONLINE.getKey() == status) {
                this.state = DeviceConstant.DeviceState.ONLINE;
            } else {
                this.state = DeviceConstant.DeviceState.OFFLINE;
            }
        }

        public Payload(JSONObject jsonObject) {
            String channelId = jsonObject.getStr("gbId");
            String name = jsonObject.getStr("name");
            String gbId = jsonObject.getStr("gbId");
            String status = jsonObject.getStr("state");
            this.channelId = channelId;
            this.name = name;
            this.gbId = gbId;
            if (AbstractQiNiuCloud.QiniuDeviceState.ONLINE.getValue().equals(status)) {
                this.state = DeviceConstant.DeviceState.ONLINE;
            } else {
                this.state = DeviceConstant.DeviceState.OFFLINE;
            }
        }
    }
}
